package cn.edu.sdjzu.xg.eagle.Service;

import cn.edu.sdjzu.xg.eagle.dao.TeacherDao;
import cn.edu.sdjzu.xg.eagle.dao.UserDao;
import cn.edu.sdjzu.xg.eagle.entity.Pagination;
import cn.edu.sdjzu.xg.eagle.entity.Teacher;
import cn.edu.sdjzu.xg.eagle.entity.User;
import cn.edu.sdjzu.xg.eagle.Service.TeacherServiceInterface;
import cn.edu.sdjzu.xg.eagle.Service.UserService;
import cn.edu.sdjzu.xg.eagle.exception.BysjException;
import cn.edu.sdjzu.xg.eagle.util.Condition;
import cn.edu.sdjzu.xg.eagle.util.JdbcHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

public class TeacherService implements TeacherServiceInterface{
    private static TeacherDao teacherDao = TeacherDao.getInstance();
    private static UserService userService = UserService.getInstance();
    private static TeacherService teacherService = new TeacherService();

    private TeacherService() {}
    public static TeacherService getInstance() {
        return teacherService;
    }


    @Override
    public boolean add(Teacher teacher) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        boolean added = false;
        try{
            Long teacherId = teacherDao.add(teacher,connection);
            teacher.setId(teacherId);
            User user = new User(teacher.getNo(),teacher.getNo(),teacher);
            added = userService.add(user,connection);
            connection.commit();
        }catch(SQLException e){
            connection.rollback();
            System.out.println(e.getMessage());
        }finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return added;
    }

    @Override
    public boolean delete(Teacher teacher) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        connection.setAutoCommit(false);
        boolean deleted = false;
        try{
            String username = teacher.getNo();
            boolean t1 =  UserDao.getInstance().delete(username,connection);
            boolean t2 = TeacherDao.getInstance().delete(teacher,connection);
            deleted = t1&&t2?true:false;
            connection.commit();
        }catch(SQLException e){
            connection.rollback();
            System.out.println(e.getMessage());
        }finally {
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return deleted;
    }

    @Override
    public Collection<Teacher> findAll() throws SQLException {
        Collection<Teacher> teachers = new HashSet<>();
        Connection connection = JdbcHelper.getConn();
        String sql = "select * from teacher";
        PreparedStatement ps = connection.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        while(rs.next()){
            Long id = rs.getLong("id");
            String name = rs.getString("name");
            String no = rs.getString("no");
            Teacher teacher = new Teacher(name,no,id);
            teachers.add(teacher);
        }
        return teachers;
    }
    @Override
    public Collection<Teacher> findAll(List<Condition> conditionList) throws SQLException, BysjException {
        Connection connection = JdbcHelper.getConn();
        Collection<Teacher> teachers = TeacherDao.getInstance().findAll(connection,conditionList);
        JdbcHelper.close(connection);
        return teachers;
    }
    @Override
    public Collection<Teacher> findAll(List<Condition>conditionList, Pagination pagination) throws SQLException, BysjException{
        Collection<Teacher> teachers=TeacherDao.getInstance().findAll(conditionList,pagination);
        return teachers;
    }

    public static void main(String[] args) throws SQLException {
        Teacher teacher = new Teacher("曾令轩","01",7L);
        TeacherService.getInstance().add(teacher);
        Teacher teacher1 = new Teacher("佳佳","02",8L);
        TeacherService.getInstance().add(teacher1);
        //TeacherService.getInstance().delete(teacher);
        //TeacherService.getInstance().delete(teacher1);

    }
}
